﻿using FRMS.DAL;
using FRMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.BUS
{
    public class DataClearBUS
    {
        public ResponseMessage QueryAllTable()
        {
            ResponseMessage message = new ResponseMessage();
            DataClearDAL da = new DataClearDAL();
            message.errcode = 0;
            message.data = da.GetAllTable();
            message.total = da.GetAllTableCount();

            return message;
        }

        public ResponseMessage ClearTable(string tablename)
        {
            ResponseMessage message = new ResponseMessage();
            DataClearDAL da = new DataClearDAL();
            if (da.DelData(tablename))
            {
                message.errcode = 0;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "清空数据失败";
            }

            return message;
        }

        public bool AddErrorLog(ErrorLog errorLog)
        {
            bool result;
            try
            {
                errorLog.ErrorMsg = errorLog.ErrorMsg.Replace('\'', ' ').Replace('\'', ' ');
                if (errorLog.ErrorMsg.Length > 500)
                {
                    errorLog.ErrorMsg = errorLog.ErrorMsg.Substring(0, 500);
                }
                DataDAL dataDAL = new DataDAL();
                result = dataDAL.AddErrorLog(errorLog);
            }
            catch(Exception e)
            {
                result = false;
            }
            return result;
        }

        public ResponseMessage QueryErrorLog(ErrorLog errorLog)
        {
            ResponseMessage message = new ResponseMessage();
            DataDAL da = new DataDAL();
            message.errcode = 0;
            message.data = da.QueryErrorLog(errorLog);
            message.total = da.QueryErrorLogCount(errorLog);

            return message;
        }

        public ResponseMessage ClearOwner(int projectid)
        {
            ResponseMessage message = new ResponseMessage();
            DataClearDAL dataClearDAL = new DataClearDAL();
            bool resut = dataClearDAL.ClearOwner(projectid);
            if (resut)
            {
                message.errcode = 0;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "清除失败";
            }
            return message;
        }

        public ResponseMessage ClearDevice(int projectid)
        {
            ResponseMessage message = new ResponseMessage();
            DataClearDAL dataClearDAL = new DataClearDAL();
            bool resut = dataClearDAL.ClearDevice(projectid);
            if (resut)
            {
                message.errcode = 0;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "清除失败";
            }
            return message;
        }

        public ResponseMessage ClearElevator(int projectid)
        {
            ResponseMessage message = new ResponseMessage();
            DataClearDAL dataClearDAL = new DataClearDAL();
            bool resut = dataClearDAL.ClearElevator(projectid);
            if (resut)
            {
                message.errcode = 0;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "清除失败";
            }
            return message;
        }

        public ResponseMessage ClearRoom(int projectid)
        {
            ResponseMessage message = new ResponseMessage();
            DataClearDAL dataClearDAL = new DataClearDAL();
            bool resut = dataClearDAL.ClearRoom(projectid);
            if (resut)
            {
                message.errcode = 0;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "清除失败";
            }
            return message;
        }

        public ResponseMessage ClearBuild(int projectid)
        {
            ResponseMessage message = new ResponseMessage();
            DataClearDAL dataClearDAL = new DataClearDAL();
            bool resut = dataClearDAL.ClearBuild(projectid);
            if (resut)
            {
                message.errcode = 0;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "清除失败";
            }
            return message;
        }
    }
}
